% by YLX,clq on 6/15
% Version 0.1
%% 画图部分需要改进

function figure10(j_all_all)
    figure(10)    
    global I J ci cI r CURRENT
    ci_origin = ci;
    cI_origin = cI;
    r_origin = r;

    cI = 100;
    r = 100;
    cis = 40:10:90;
    N = size(j_all_all, 2); % 可否这样写省去N参数?
    len_seq = size(j_all_all, 1); %认为j_all_all为seq行向量的组合
    len_cis = size(cis, 2);
    percentages = zeros(len_cis, I); % average

    for ii = 1:len_seq
        if mod(ii, 5) == 0
            fprintf("\t figure10 -> Seq No. %d  \n", ii);
        end
        Line = j_all_all(ii, :);
        for jj = 1:len_cis
            % 这里大段代码重复 未来可以打包成函数
            % 代码来自 prop_policy.m
            ci = cis(jj);
            S = zeros(I, J);
            for n = 1:N 
                CURRENT = n; % 用CURRENT控制了findQ的行为
                j = Line(n);
                profits = zeros(1, I);
                for i = 1:I
                    S_test = S;
                    S_test(i, j) = S_test(i, j) + 1; 
                    Q = findQ(S_test); % size(Q) = [I, CURRENT]
                    R = findR(Q);
                    profits(i) = function_fQR(Q, R);
                end
                i = find(profits == max(profits));
                i = i(1);
                S(i, j) = S(i, j) + 1;
                Q = findQ(S);
                % R = findR(Q); % R看起来对这个程序没用
            end
            sum_num = sum(S, 2);
            for i = 1:I 
                percentages(jj, i) = percentages(jj, i) + sum_num(i); % 先循环叠加再求占比和平均，可以减小误差
            end
        end
    end
    percentages = percentages / len_seq / N;

    % >>> Need a Ploter Here
    clf;
    for i = 1:I
        if mod(i,2)==0
            plot(cis, 100*percentages(:,i),'--k');
        else
            plot(cis, 100*percentages(:,i),'k');
        end
        hold on
    end
    xlabel("c_i");
    ylabel("Percentage of patients assigned");
    legend('1','2','3','4','5','6','7','8','Location','Best')
    ylim([6 18])
    % DRAWBRACE([88,11.5],[88,12.3],5) 网上查到可以，但是用起来不行
    text(87,13,'\downarrow 2-7')
    text(87,16,'\downarrow 1')
    text(87,14,'\uparrow 8')
    grid on
    % <<< 

    % >>> 恢复global变量的值（如果后续没用到原来的值，不写也行）
    ci = ci_origin;
    cI = cI_origin;
    r = r_origin;
    % <<<
    return;

